package keter.web.controller.admin;

import javax.servlet.http.HttpServletRequest;

import keter.framework.web.util.MediaType;
import keter.model.Menu;
import keter.service.MenuService;
import keter.web.controller.base.KeterBaseController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@PreAuthorize("hasRole('ADMIN')")
@RequestMapping("/keter/admin/menu")
@RestController
public class MenuController extends KeterBaseController {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);
    
    /**
     * 进入菜单管理页面
     * @param request
     * @return 
     * @author  gulixing@msn.com
     * @date    2015年12月21日
     */
    @RequestMapping(value=Routes.Index, method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF_8)
    public String index(HttpServletRequest request) {
    	return getDefaultPath()+Routes.Index;
    }
    
    @GetMapping(value=Routes.GetAll,produces = MediaType.APPLICATION_JSON_UTF_8)//
    public String get() {
    	return toJson(Menu.dao.findAll()) ;
    }
    
    @GetMapping(value=Routes.Get)//consumes = MediaType.APPLICATION_JSON_UTF_8
    public String getOne(@PathVariable String id) {
    	return toJson(Menu.dao.findById(id)) ;
    }
    
    @RequestMapping(value=Routes.Add, method = RequestMethod.POST)
    public String add(
       		@RequestParam(value="label")String label,
    		@RequestParam(value="url")String url,
    		@RequestParam(value="icon",required=false)String icon,
    		@RequestParam(value="parent_id",required=false)String parentId
    	) {
    	Menu menu = new Menu()
    	.set("label", label)
    	.set("url", url)
    	.set("icon", icon)
    	.set("parent_id", parentId)
    	;
    	MenuService.service.save(menu);
    	return success(menu.getID().toString());
    }
    
    @RequestMapping(value=Routes.Update, method = RequestMethod.POST)//consumes = MediaType.APPLICATION_JSON_UTF_8
    public String update(
    		@RequestParam(value="id")String id,
    		@RequestParam(value="label",required=false)String label,
    		@RequestParam(value="url",required=false)String url,
    		@RequestParam(value="icon",required=false)String icon
    		) {
    	Menu menu = Menu.dao.findById(id);
    	menu.set("id", id)
    	    .set("label", label)
    	    .set("url", url)
    	    .set("icon", icon);
    	if(menu.update()){
    		return success();
    	}
    	return error("更新失败!");
    }
    
    @RequestMapping(value=Routes.Delete, method = RequestMethod.POST)//consumes = MediaType.APPLICATION_JSON_UTF_8
    public String delete(String id) {
    	Menu.dao.deleteByRootId(id);
    	return success();
    }

}